package cn.interceptor;

import javax.servlet.http.HttpServletRequest;

import org.apache.struts2.ServletActionContext;
import org.apache.struts2.convention.annotation.ParentPackage;
import org.mybatis.spring.MyBatisSystemException;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import cn.exception.AhCustomException;
import cn.exception.AhCustomException.ExcCode;

import com.opensymphony.xwork2.Action;
import com.opensymphony.xwork2.ActionInvocation;
import com.opensymphony.xwork2.interceptor.AbstractInterceptor;

/**
 * 
 * @项目名称:pigbaby
 * @类名称:ExceptionInterceptor
 * @类描述:
 * @创建人:陈凯
 * @创建时间:20150713 17：19 星期一
 * @修改人:
 * @修改时间:
 * 
 * @修改备注:
 * @version
 */
@ParentPackage(value="struts-default")
public class ExceptionInterceptor extends AbstractInterceptor{
	public static Logger logger = LoggerFactory.getLogger(ExceptionInterceptor.class);
	private static final long serialVersionUID = -3490533736557683231L;
	private String excMessage="";
	@Override
	public String intercept(ActionInvocation invocation) throws Exception {
		String result;
		HttpServletRequest request = ServletActionContext.getRequest();
		String uri = request.getRequestURI();
		try {
			result = invocation.invoke();
		} catch (MyBatisSystemException e) {

			e.printStackTrace();
			logger.error("异常拦截器拦截到异常:"+"<br/>"+"uri为:"+uri+"<br/>"+e);
			excMessage=AhCustomException.getExcMessage(ExcCode.DataProcessing);
			request.setAttribute("msg", excMessage);
			result=Action.ERROR;
		}catch (NullPointerException e) {
			e.printStackTrace();
			logger.error("异常拦截器拦截到异常:"+"<br/>"+"action的名称为:"+uri+"<br/>"+e);
			excMessage=AhCustomException.getExcMessage(ExcCode.IllegalData);
			request.setAttribute("msg", excMessage);
			result=Action.ERROR;
		}catch (AhCustomException e) {
			e.printStackTrace();
			logger.error("异常拦截器拦截到异常:"+"<br/>"+"action的名称为:"+uri+"<br/>"+e);
			excMessage=e.getExcMessage();
			request.setAttribute("msg", excMessage);
			result=Action.ERROR;
		}catch (Exception e) {
			e.printStackTrace();
			logger.error("异常拦截器拦截到异常:"+"<br/>"+"action的名称为:"+uri+"<br/>"+e);
			excMessage=AhCustomException.getExcMessage(ExcCode.AppError);
			request.setAttribute("msg", excMessage);
			result=Action.ERROR;
		}
		System.out.println(excMessage);
		return result;
	}

}
